package com.inf.utils;

import net.sf.json.JSONObject;
import okhttp3.*;
import org.apache.commons.fileupload.disk.DiskFileItem;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

/**
 * Created by zhangbn on 2018/11/12.
 */
public class FeatureUtil {

    public static JSONObject getFeature(String filePath) throws IOException {
        OkHttpClient client = new OkHttpClient();

        File file = new File(filePath);
        RequestBody fileBody = RequestBody.create(MediaType.parse("image/*"), file);

        RequestBody requestBody = new MultipartBody.Builder()
                .setType(MultipartBody.FORM)
                .addFormDataPart("imageData", "imageData", fileBody)
                .build();

        Request request = new Request.Builder()
                .url("http://10.82.25.181:9001/verify/feature/gets")
                .post(requestBody)
                .build();

        Response response = client.newCall(request).execute();

        JSONObject jsonObject = JSONObject.fromObject(response.body().string());
        response.close();
        return jsonObject;
    }


    public static String getFeatureStrByMultipartFile(MultipartFile pic) throws IOException {
        OkHttpClient client = new OkHttpClient();

        CommonsMultipartFile cf = (CommonsMultipartFile) pic;
        DiskFileItem fi = (DiskFileItem) cf.getFileItem();
        File file = fi.getStoreLocation();

        okhttp3.RequestBody fileBody = okhttp3.RequestBody.create(MediaType.parse("image/*"), file);

        okhttp3.RequestBody requestBody = new MultipartBody.Builder()
                .setType(MultipartBody.FORM)
                .addFormDataPart("imageData", "imageData", fileBody)
                .build();

        Request request = new Request.Builder()
                .url("http://10.82.25.181:9001/verify/feature/gets")
                .post(requestBody)
                .build();

        Response response = client.newCall(request).execute();

        JSONObject jsonObject = JSONObject.fromObject(response.body().string());
        response.close();

        deleteFile(file);
        return jsonObject.getString("feature");
    }


    /**
     * File.createTempFile(String prefix, String suffix); 创建一个临时File对象，或者new File()
     * MultipartFile.transferTo(File dest); 实现 MultipartFile to File
     *
     * @param multfile
     * @return
     * @throws Exception
     */
    public File multipartFile2File(MultipartFile multfile) throws Exception {
        // 获取文件名
        String fileName = multfile.getOriginalFilename();
        // 获取文件后缀
        String prefix = fileName.substring(fileName.lastIndexOf("."));
        // 用uuid作为文件名，防止生成的临时文件重复
        final File excelFile = File.createTempFile(UUID.randomUUID().toString().replace("-", ""), prefix);
        // MultipartFile to File
        multfile.transferTo(excelFile);

        //程序结束时，删除临时文件
        deleteFile(excelFile);
        return excelFile;
    }

    /**
     * 删除
     *
     * @param files
     */
    private static void deleteFile(File... files) {
        for (File file : files) {
            if (file.exists()) {
                file.delete();
            }
        }
    }
}
